/***********************************************************************
 * POJO Class :  SysMenuResource.java
 * Author:  Bill Vison
 * Purpose: Defines the Class SysMenuResource
 ***********************************************************************/

package edu.gtcfla.km.model;

import java.util.Iterator;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Transient;

import org.vsg.biz.utils.InjectRepo;

import edu.gtcfla.km.Knowledge;
import edu.gtcfla.km.UploadFile;
import edu.gtcfla.km.repositories.UploadFileRepo;

@javax.persistence.Entity
@javax.persistence.Table(name = "KNOWLEDGE")
@PrimaryKeyJoinColumn(name="DOC_ID")
public class KnowledgeD extends DocD implements Knowledge {


	private static final long serialVersionUID = -3883186021751223092L;

	@Column(name ="CONTENT")	
	private String content;
	
	@InjectRepo
	@Transient
	private UploadFileRepo<UploadFile , String> uploadFileRepo;	
	
	public UploadFileRepo<UploadFile, String> getUploadFileRepo() {
		return uploadFileRepo;
	}

	public void setUploadFileRepo(UploadFileRepo<UploadFile, String> uploadFileRepo) {
		this.uploadFileRepo = uploadFileRepo;
	}

	@Override
	public String getContent() {
		// TODO Auto-generated method stub
		return content;
	}

	@Override
	public void setContent(String content) {
		// TODO Auto-generated method stub
		this.content  =content;
	}

	@Column(name ="KEYWORDS")	
	private String keywords;
	
	@Override
	public String getKeywords() {
		// TODO Auto-generated method stub
		return keywords;
	}

	@Override
	public void setKeywords(String keyword) {
		// TODO Auto-generated method stub
		this.keywords = keyword;
	}
	
	@OneToMany(
			mappedBy="knowledgeD",
			cascade = {CascadeType.ALL},
			fetch = FetchType.LAZY)	
	private Set<UploadFileD> attchmentsD;

	
	public Set<UploadFileD> getAttchmentsD() {
		return attchmentsD;
	}

	public void setAttchmentsD(Set<UploadFileD> attchmentsD) {
		this.attchmentsD = attchmentsD;
	}

	@Override
	public Set<UploadFile> getAttachments() {
		// TODO Auto-generated method stub
		Set<UploadFile> atts =  (Set<UploadFile>)(Set)getAttchmentsD();

		try {
			
			Iterator<UploadFile> attIter = atts.iterator();
			
			while (attIter.hasNext()) {
				UploadFile uf = attIter.next();
			}

			
		} catch (org.hibernate.LazyInitializationException e) {
			
			// --- find attachment by doc id ---
			String docId = this.getId() ;
			atts =( Set<UploadFile>) uploadFileRepo.findAllByKnowledge( docId );

			return atts;

		}		
		
		return atts;
	}

	@Override
	public void setAttachments(Set<UploadFile> files) {
		// TODO Auto-generated method stub
		
		setAttchmentsD( (Set<UploadFileD>)(Set)files );
	}
	



}